package com.jic.product.controller.manage;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.entity.CategoryTreeNodeRelation;
import com.jic.product.entity.ProductUnits;
import com.jic.product.manage.request.CategoryTreeNodeRelationRequest;
import com.jic.product.manage.request.ProductUnitsEditRequest;
import com.jic.product.manage.request.ProductUnitsRequest;
import com.jic.product.manage.response.CategoryTreeNodeRelationResponse;
import com.jic.product.manage.response.ProductUnitsResponse;
import com.jic.product.mapper.CategoryTreeNodeRelationMapper;
import com.jic.product.service.CategoryTreeNodeRelationService;
import com.jic.product.service.ProductUnitsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by ZXK on 2020/1/9 11:04
 * 类目节点聚合表
 */
@Slf4j
@RestController
@RequestMapping("/categoryTreeNodeRelation")
@Api(value = "类目节点聚合", description = "类目节点聚合", tags = "类目节点聚合")
public class CategoryTreeNodeRelationController {

    @Autowired
    private CategoryTreeNodeRelationService categoryTreeNodeRelationService;




    /*
     *  类目节点聚合新增
     *
     * */
    @ResponseBody
    @PostMapping(value = "/insertCategoryTreeNodeRelation")
    public RestResult insertCategoryTreeNodeRelation(@RequestBody CategoryTreeNodeRelationRequest categoryTreeNodeRelationRequest) {
        log.info("开始进入类目节点聚合新增=========》,{}", categoryTreeNodeRelationRequest.toString());
        try {
            CategoryTreeNodeRelation categoryTreeNodeRelation=BeanUtil.copy(categoryTreeNodeRelationRequest,CategoryTreeNodeRelation.class);
            //新增之前判断是否已经聚合后台类目
            List<CategoryTreeNodeRelationResponse> categoryTreeNodeRelationResponseList1=categoryTreeNodeRelationService.query(categoryTreeNodeRelation);
            if(categoryTreeNodeRelationResponseList1.size()!=0){
                return RestResult.error("9999","前台节点和后台节点已经聚合");
            }
            List<CategoryTreeNodeRelationResponse> categoryTreeNodeRelationResponseList=categoryTreeNodeRelationService.insert(categoryTreeNodeRelation);
            return RestResult.success(categoryTreeNodeRelationResponseList);
        } catch (Exception e) {
            log.error("insertCategoryTreeNodeRelation========》",e.getLocalizedMessage());
            return  RestResult.error("9999",e.getLocalizedMessage());
        }
    }
    /*
     *  查询已聚合的类目
     *
     * */
    @ResponseBody
    @PostMapping(value = "/queryCategoryTreeNodeRelation")
    public RestResult queryCategoryTreeNodeRelation(@RequestBody CategoryTreeNodeRelationRequest categoryTreeNodeRelationRequest) {
        RestResult restResult = new RestResult<>();
        log.info("开始调用查询已聚合的类目=========》,{}", categoryTreeNodeRelationRequest.toString());
        try {
            CategoryTreeNodeRelation categoryTreeNodeRelation=BeanUtil.copy(categoryTreeNodeRelationRequest,CategoryTreeNodeRelation.class);
            List<CategoryTreeNodeRelationResponse> categoryTreeNodeRelationResponseList=categoryTreeNodeRelationService.query(categoryTreeNodeRelation);
            return RestResult.success(categoryTreeNodeRelationResponseList);
        } catch (Exception e) {
            log.error("queryCategoryTreeNodeRelation========》",e);
        }
        return restResult;

    }

    /*
     *  删除已聚合的列表
     *
     * */
    @ResponseBody
    @GetMapping(value = "/deleteCategoryTreeNodeRelation")
    public RestResult deleteCategoryTreeNodeRelation(@RequestParam String  ids) {
        log.info("开始调用删除已聚合的列表=========》,{}", ids);
        RestResult restResult = new RestResult<>();
        try {
           String[] arrIds=ids.split(",");
           for(int i=0;i<arrIds.length;i++){
               categoryTreeNodeRelationService.delete(Long.valueOf(arrIds[i]));
           }
           return  RestResult.success(true);
        } catch (Exception e) {
            log.error("deleteCategoryTreeNodeRelation========》",e);
        }
        return restResult;

    }
}
